package com.example.project.service.impl;

import com.example.project.common.MyTool;
import com.example.project.dao.SafetyTrainBinDao;
import com.example.project.dao.SysMessageDao;
import com.example.project.entity.SafetyTrain;
import com.example.project.dao.SafetyTrainDao;
import com.example.project.entity.SafetyTrainBin;
import com.example.project.entity.SysMessage;
import com.example.project.service.SafetyTrainService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (SafetyTrain)表服务实现类
 *
 * @author makejava
 * @since 2024-02-23 15:18:23
 */
@Service("safetyTrainService")
public class SafetyTrainServiceImpl implements SafetyTrainService {
    @Resource
    private SafetyTrainDao safetyTrainDao;
    @Resource
    private SafetyTrainBinDao safetyTrainBinDao;

    @Resource
    private SysMessageDao sysMessageDao;

    private MyTool myTool = new MyTool();
    /**
     * 通过ID查询单条数据
     *
     * @param safetyTrainId 主键
     * @return 实例对象
     */
    @Override
    public SafetyTrain queryById(Integer safetyTrainId) {
        return this.safetyTrainDao.queryById(safetyTrainId);
    }

    /**
     * 分页查询
     *
     * @param safetyTrain 筛选条件
     * @param pageRequest 分页对象
     * @param startTime
     * @param endTime
     * @param day
     * @param nextDay
     * @return 查询结果
     */
    @Override
    public Page<SafetyTrain> queryByPage(SafetyTrain safetyTrain, PageRequest pageRequest, Date startTime, Date endTime, Date day, Date nextDay) {
        long total = this.safetyTrainDao.count(safetyTrain);

        return new PageImpl<>(this.safetyTrainDao.queryAllByLimit(safetyTrain, pageRequest, startTime, endTime, day, nextDay), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param safetyTrain 实例对象
     * @return 实例对象
     */
    @Override
    public SafetyTrain insert(SafetyTrain safetyTrain) {
        //生成创建时间
        safetyTrain.setCreationTime(myTool.dateNow());
        this.safetyTrainDao.insert(safetyTrain);
        return safetyTrain;
    }

    /**
     * 修改数据
     *
     * @param safetyTrain 实例对象
     * @return 实例对象
     */
    @Override
    public SafetyTrain update(SafetyTrain safetyTrain) {
        //生成修改时间
        safetyTrain.setUpdateTime(myTool.dateNow());
        this.safetyTrainDao.update(safetyTrain);
        return this.queryById(safetyTrain.getSafetyTrainId());
    }

    /**
     * 通过主键删除数据
     *
     * @param safetyTrainId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer safetyTrainId) {
        return this.safetyTrainDao.deleteById(safetyTrainId) > 0;
    }

    @Override
    public SafetyTrain insert1(SafetyTrain safetyTrain) {
        //创建时间
        safetyTrain.setCreationTime(myTool.dateNow());
        //未审核
        safetyTrain.setAudit(1);
        //更新时间
        safetyTrain.setUpdateTime(myTool.dateNow());
        safetyTrain.setEntRecBin(1);
        return this.insert(safetyTrain);
    }
/**
 * 删除成功进入回收站
 * 失败
 */
    @Override
    public boolean deleteById1(Integer safetyTrainId) {
        SafetyTrain safetyTrain = this.safetyTrainDao.queryById(safetyTrainId);
        safetyTrain.setEntRecBin(2);
        //创建回收站数据
        SafetyTrainBin safetyTrainBin = new SafetyTrainBin();
        safetyTrainBin.setSafetyTrainId(safetyTrainId);
        safetyTrainBin.setBinTime("0,1");
        //生成当前删除时的时间
        safetyTrainBin.setDeleteTime(myTool.dateNow());
        //绑定
        this.safetyTrainBinDao.insert(safetyTrainBin);

        return this.safetyTrainDao.update(safetyTrain)>0;
    }

    @Override
    public Boolean deleteByIds(String ids) {
        boolean flog = false;
        if (ids.isEmpty()){
            return flog;
        }
        List<Integer> list = myTool.myToList(ids);
        for (Integer e:list){
            this.deleteById1(e);
        }
        return true;
    }

    @Override
    public SafetyTrain update1(SafetyTrain data) {
        //数据库里的数据
        SafetyTrain safetyTrain = this.safetyTrainDao.queryById(data.getSafetyTrainId());
        //报警信息
        String pj = null;

        Integer x = data.getAudit();
        if(x != null){
            switch (x){
                case 2:{
                    pj = safetyTrain.getTrainingTheme()+"被驳回";
                    break;}
                case  3:{
                   pj = safetyTrain.getTrainingTheme()+"审核通过";
                    break;}
                default:break;
            }
        }

        if (pj != null){
            SysMessage sysMessage = this.myTool.generateMessage(pj);
            this.sysMessageDao.insert(sysMessage);
        }
        return this.update(data);
    }
}
